(function(){
	var ADVANCED_SEARCH_REG = /[\s+\.\:]/;

/**
 * @class Bible.form.BibleBooksComboBox
 * @extends Ext.form.ComboBox
 */	
Bible.form.BibleBooksComboBox = Ext.extend(Ext.form.ComboBox, {
    
	infoTooltip: "scrie Cartea, capitolul şi versetul <br/>(ex. <span class='bold'>'Matei 5'</span>, <span class='bold'>'Mat 5.3'</span>, sau <span class='bold'>'Mat 5:3'</span>)",
	
	width: 150,
    displayField:'name',
    valueField: 'id',
    mode: 'local',
    allowBlank: false,
    selectOnFocus: true,
    lastQuery: '',
    lazyInit: false,
    lazyRender: false,
    forceSelection: true,
    //typeAhead: true,
    triggerAction: 'all',
    editable: true,
    listClass: 'x-combo-list-small x-combo-bible-books',
//	listWidth: 412,
//    maxHeight: 440,
    tpl: '<tpl for="."><div class="x-combo-list-item"><div>{id}</div>{name}</div></tpl>',
	
    initComponent : function(){
        if(!this.store){
            console.debug('store auto created');
            this.store = new Bible.data.BibleBooksArrayStore();
        }
        Bible.form.BibleBooksComboBox.superclass.initComponent.call(this);
    },
	
	initEvents: function(){
		Bible.form.BibleBooksComboBox.superclass.initEvents.call(this);
		
		if(this.infoTooltip){		
			new Ext.ToolTip({
				target: this.wrap,
				delegate: '.x-form-trigger',
				html: this.infoTooltip,
				anchor: 'br-tr',
				mouseOffset: [-(this.width - 17), 0],
				renderTo: document.body,
				dismissDelay: 8000
			});
		}
	},
	
	doQuery : function(q, forceAll){
        q = Ext.isEmpty(q) ? '' : q;
        var qe = {
            query: q,
            forceAll: forceAll,
            combo: this,
            cancel:false
        };
        if(this.fireEvent('beforequery', qe)===false || qe.cancel){
            return false;
        }
        q = qe.query;
		if(q.length > 1 && q[1] != ' ' && Ext.isNumber(q[0]*1)){
			console.debug('insert space after first number', q);
			q = q.insert(1, ' ');
			console.debug('new query after space inserted', q);
		}
		var s = q.split(ADVANCED_SEARCH_REG),
			chapter, verse,
			bookExtraParams = false,
			len = s.length,
			startParamIdx = 1;
		console.debug('query', q, s);
		if(len > 1){
			if(Ext.isNumber(s[0]*1)){
				q = s[0] + ' ' + s[1];
				console.debug('concat values', q);
				startParamIdx = 2;
			} else {
				q = s[0];
			}
			if(len > startParamIdx){
				chapter = Ext.num(parseInt(s[startParamIdx]), 0);
				if(len > startParamIdx + 1){
					verse = Ext.num(parseInt(s[startParamIdx + 1], 0));
				}
			}
			if(chapter){
				bookExtraParams = {
					chapter: chapter,
					verse: verse
				};
			}
			console.debug('new query', q);
		}
		this.bookExtraParams = bookExtraParams;
        forceAll = qe.forceAll;
        if(forceAll === true || (q.length >= this.minChars)){
            if(this.lastQuery !== q){
                this.lastQuery = q;
                if(this.mode == 'local'){
                    this.selectedIndex = -1;
                    if(forceAll){
                        this.store.clearFilter();
                    }else{
                        this.store.filter(this.displayField, q, true, false, false);
                    }
                    this.onLoad();
                }else{
                    this.store.baseParams[this.queryParam] = q;
                    this.store.load({
                        params: this.getParams(q)
                    });
                    this.expand();
                }
            }else{
                this.selectedIndex = -1;
                this.onLoad();
            }
        }
    }
});

	Ext.reg('biblebookscombobox', Bible.form.BibleBooksComboBox);

})();
